<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Mcontent extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    
    public function getChannel()
    {
        $sql = "SELECT * FROM channel ORDER BY id DESC";
        $data = $this->db->query($sql)->result_object();
        return $data;
    }
    
    public function addChannelDb($channel,$img)
    {
        $data = array(
            'name' => $channel,
            'logo' => $img
        );
        //die(print_r($data));
        $count = $this->db->query('SELECT id FROM channel WHERE name = "'.$channel.'"')->num_rows();
        if($count == 0){
            $this->db->insert('channel', $data);
            return 200;
        }else{
            @unlink(PROJECT_PATH.'/data/'.$img);
        }
        return 201;
    }
    
    public function getChannelInfo($id)
    {
        $data = $this->db->query('SELECT * FROM channel WHERE id = "'.$id.'"')->row_array();
        return $data;
    }
    public function editChannelDb($channel,$img,$id)
    {
        $data = array(
            'name' => $channel
        );
        if(!in_array($img, array(null, '', '0')))
        {
            $data['logo'] = $img;
        }
        //die(print_r($data));
        
        $this->db->update('channel', $data, "id = '".$id."'");
        return 200;
    }
    
    public function deleteChannelDb($id)
    {
        $img = $this->db->query("SELECT logo FROM channel WHERE id = '".$id."'")->row_array();
        @unlink(PROJECT_PATH.'/data/'.$img['logo']);
        
        $this->db->delete('channel', array('id' => $id)); 
        return 200;
    }
    
    
    public function getSelectChannel()
    {
        $sql = "SELECT * FROM channel";
        $data = $this->db->query($sql)->result_array();
        return $data;
    }
    public function getTVShow($channel_id)
    {
		if( $channel_id && $channel_id !="" ){
			$sql = "SELECT tv.*,ch.name AS channel FROM tvshow AS tv LEFT JOIN channel AS ch ON tv.channel_id = ch.id WHERE ch.id = ".$channel_id."  ORDER BY tv.id DESC";
		}else{
			$sql = "SELECT tv.*,ch.name AS channel FROM tvshow AS tv LEFT JOIN channel AS ch ON tv.channel_id = ch.id ORDER BY tv.id DESC";
		}
        
        $data = $this->db->query($sql)->result_object();
        return $data;
    }
    
    public function addTVShowDb($tvshow,$channel,$time,$img)
    {
        $date = date('Y-m-d H:i:s');
        $tvshow_ascii = $this->get_ascii($tvshow);
        $data = array(
            'name' => $tvshow,
            'name_ascii' => $tvshow_ascii,
            'background' => $img,
            'channel_id' => $channel,
            'schedule' => strtotime($time),
            'active' => 1,
            'created' => strtotime($date)
        );
        //die(print_r($data));
        $this->db->insert('tvshow', $data);
        return 200;
    }
    
    public function getTVShowInfo($id)
    {
        $data = $this->db->query('SELECT * FROM tvshow WHERE id = "'.$id.'"')->row_array();
        return $data;
    }
    public function editTVShowDb($tvshow,$channel,$time,$img,$id)
    {
        $date = date('Y-m-d H:i:s');
        $tvshow_ascii = $this->get_ascii($tvshow);
        $data = array(
            'name' => $tvshow,
            'name_ascii' => $tvshow_ascii,
            'channel_id' => $channel,
            'schedule' => strtotime($time),
            'active' => 1,
            'created' => strtotime($date)
        );
        if(!in_array($img, array(null, '', '0')))
        {
            $data['background'] = $img;
        }
        //die(print_r($data));
        
        $this->db->update('tvshow', $data, "id = '".$id."'");
        return 200;
    }
    
    public function deleteTVShowDb($id)
    {
        $img = $this->db->query("SELECT background FROM tvshow WHERE id = '".$id."'")->row_array();
        @unlink(PROJECT_PATH.'/data/'.$img['background']);
        
        $this->db->delete('tvshow', array('id' => $id)); 
        return 200;
    }
    
    public function addVote($question, $choice, $id_tvshow, $isshare, $id_log)
    {
        $q = array('question' => $question);
        $as = array();
        $i = 1;
        foreach($choice AS $el=>$le)
        {
            $as['id_'.$i] = array($choice[$el]);
            $i++;
        }
        
        $x = array('choice' => $as);
        
        $arr = json_encode(array_merge($q, $x));
        
        $data = array(
            'content'           => $arr,
            'tvshow_id'         => $id_tvshow,
            'created'           => time(),
            'isshare'           => $isshare,
            'id_log'           => $id_log
        );
        $this->db->insert('questions', $data);
        $id = $this->db->insert_id();
        return (object)array('stt' => 200, 'id' => $id);
    }
    
    public function getVoteListDb($id)
    {
        $data = $this->db->query('SELECT * FROM questions WHERE id = '.$id)->row_object();
        $data->id_channel = $this->db->query('SELECT c.id AS cid FROM channel AS c INNER JOIN tvshow AS ts ON ts.channel_id = c.id WHERE ts.id = '.$data->tvshow_id)->row_object()->cid;
        return $data;
    }
    
    public function deleteVoteDb($id)
    {
        $this->db->delete('questions', array('id' => $id)); 
        return 200;
    }
}
